import time

from selenium.webdriver.common.by import By
from selenium.webdriver import ActionChains


class LoginOperation:

    def login(self, driver, name, pwd):
        driver.find_element(By.XPATH, '//*[@id="userNname"]').send_keys(name)

        driver.find_element(By.XPATH, '//*[@id="password"]').send_keys(pwd)

        # 定位
        target = driver.find_element(By.XPATH, '//*[@id="box"]/div[2]')

        # 定位滑块元素
        slider = driver.find_element(By.XPATH, '//*[@id="box"]/div[3]')

        # 计算水平距离
        distance = target.location['x'] - slider.location['x']

        # 创建一个ActionChains对象
        actions = ActionChains(driver)

        # 模拟拖动滑块
        actions.drag_and_drop_by_offset(slider, distance, 0).perform()

        driver.find_element(By.XPATH, '//*[@id="submit"]').click()

        time.sleep(1)

    #  获取成功的实际结果
    def getSuccessResult(self, driver):
        result = driver.find_element(By.XPATH, '//*[@id="layui-layer2"]/div[2]').text
        if result == '登陆成功!':
            return 1
        else:
            return 0
